//
//  WBVisitorView.swift
//  WeiboSwift
//
//  Created by hfwd on 2017/5/16.
//  Copyright © 2017年 hfwd. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {
    
     /// 使用字典设置访客信息 访客信息字典（imageName/message）
    var visitorInfo: [String : String]? {
        
        didSet {
            
            guard let imageName = visitorInfo?["imageName"],
                
                  let message = visitorInfo?["message"]
                
                else {
                    
                    return
            }
            
            tipLabel.text = message
            
            if imageName == "" {
                
                return
            }
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }


    
    //MARK: -私有属性
    ///图像
    lazy var iconView  = UIImageView(image: UIImage(named:"WBVisitorView_icon"))
    
    ///小房子
    lazy var houseView  = UIImageView(image: UIImage(named:"WBVisitorView_house"))
    
    ///提示标签
    lazy var tipLabel  = UILabel().createLabel(title: "关注一些人，回这里看看有什么惊喜，关注一些人，回这里看看有什么惊喜。", fontSize: 14, textColor: UIColor.darkGray)
    
    ///注册按钮
    lazy var registerButton = UIButton().createBtn(
        title: "注册",
        normalColor: UIColor.orange,
        highLightedColor: UIColor.darkText,
        bgImageName: "WBVisitorView_btn")
    
    ///登录按钮
    lazy var loginButton = UIButton().createBtn(
        title: "登录",
        normalColor: UIColor.gray,
        highLightedColor: UIColor.darkText,
        bgImageName: "WBVisitorView_btn")
}

// MARK: - 设置界面
extension WBVisitorView {
    
    func setUI() {
        
        backgroundColor = UIColor(hexColor: "0xEDEDED")
        
        addSubview(iconView)
        
        addSubview(houseView)
        
        addSubview(tipLabel)
        
        tipLabel.textAlignment = .center
        
        addSubview(registerButton)
        
        addSubview(loginButton)
        
        //取消autoResizing
        for v in subviews {
            
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //自动布局
        //图像
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -60))
        //小房子
        addConstraint(NSLayoutConstraint(item: houseView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: houseView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        
        //提示标签
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20.0))
        
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 244))
       
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .top,
                                         multiplier: 1.0,
                                         constant: 0))

        
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
    }
}
